﻿<script type="text/javascript">
    $(function () {

        getButton("user");
        //回车搜索
        $("#ui_user_search").find('input').on('keyup', function (event) {
            if (event.keyCode == '13') {
                ui_user_searchdata();
            }
        })
    });

    function ui_user_init_list(toolbar) {
        $("#ui_user_dg").datagrid({       //初始化datagrid
            url: "ashx/rm_user.ashx?action=search",
            striped: true, rownumbers: true, pagination: true, pageSize: 20,
            idField: 'id',
            sortName: 'addDate',
            sortOrder: 'desc',
            pageList: [20, 40, 60, 80, 100],
            frozenColumns: [[
                             { field: 'ck', checkbox: true },
                             {
                                 width: 100,
                                 title: '登录名',
                                 field: 'userName',
                                 sortable: true,
                                 formatter: function (value, row, index) {
                                     return value.length > 8 ? '<span title="' + value + '">' + value + '</span>' : value;
                                 }
                             }, {
                                 width: 80,
                                 title: '姓名',
                                 field: 'realName',
                                 sortable: true,
                                 formatter: function (value, row, index) {
                                     return value && value.length > 5 ? '<span title="' + value + '">' + value + '</span>' : value;
                                 }
                             }]],
            columns: [[
                       {
                           field: 'roleNames', title: '角色', width: 180,
                           formatter: function (value, row, index) {
                               return value && value.length > 12 ? '<span title="' + value + '">' + value + '</span>' : value;
                           }
                       },
                       {
                           field: 'deptNames', title: '部门', width: 150,
                           formatter: function (value, row, index) {
                               return value && value.length > 10 ? '<span title="' + value + '">' + value + '</span>' : value;
                           }
                       },
                       {
                           field: 'able', title: '启用', sortable: true, width: 60, align: 'center',
                           formatter: function (value, row, index) {
                               return value ? '<img src="themes/icons/chk_checked.gif" alt="已启用" title="用户已启用" />' : '<img src="themes/icons/chk_unchecked.gif" alt="未启用" title="用户未启用" />';
                           }
                       },
                       { field: 'addDate', title: '添加时间', sortable: true, width: 130 },
                       {
                           field: 'description', title: '简介', sortable: true, width: 260,
                           formatter: function (value, row, index) {
                               return value && value.length > 15 ? '<span title="' + value + '">' + value + '</span>' : value;
                           }
                       }
            ]],
            toolbar: toolbar.length == 0 ? null : toolbar,
            //toolbar: [{
            //    text: '添加',
            //    iconCls: 'icon-add',
            //    handler: function () { ui_user_add(); }
            //}, {
            //    text: '修改',
            //    iconCls: 'icon-edit',
            //    handler: function () { ui_user_edit(); }
            //}, {
            //    text: '删除',
            //    iconCls: 'icon-remove',
            //    handler: function () { ui_user_delete() }
            //}, '-', {
            //    text: '部门设置',
            //    iconCls: 'icon-group',
            //    handler: function () { ui_user_setdept() }
            //}, {
            //    text: '角色设置',
            //    iconCls: 'icon-key_go',
            //    handler: function () { ui_user_setrole() }
            //}],

            onDblClickRow: function (rowIndex, rowData) {     //双击行弹框编辑
                //被编辑列高亮，其他列去除高亮
                $("#ui_user_dg").datagrid('clearSelections').datagrid('clearChecked').datagrid('checkRow', rowIndex);
                ui_user_edit();
            }
        });
    }

    //添加用户
    function ui_user_add() {
        $("<div/>").dialog({
            id: "ui_user_add_dialog",
            href: "html/ui_user_edit.html",
            title: "添加用户",
            iconCls: 'icon-add',
            height: 350,
            width: 460,
            modal: true,
            buttons: [{
                id: "ui_user_add_btn",
                iconCls: 'icon-add',
                text: '添 加',
                handler: function () {
                    $("#ui_user_editform").form("submit", {
                        url: "ashx/rm_user.ashx",
                        onSubmit: function (param) {
                            $('#ui_user_add_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'add';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_user_add_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = eval('(' + data + ')');    //转成json格式
                            if (dataJson.success) {
                                $("#ui_user_add_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "添加成功！");
                                $("#ui_user_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_user_add_btn').linkbutton('enable');   //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_user_userid_add").focus();
            },
            onClose: function () {
                $("#ui_user_add_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    //修改用户
    function ui_user_edit() {
        var rows = $("#ui_user_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要修改的用户或者双击某个用户");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量修改");
            $("#ui_user_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_user_edit_dialog",
            href: "html/ui_user_edit.html",
            title: "修改用户",
            iconCls: 'icon-edit',
            height: 350,
            width: 460,
            modal: true,
            buttons: [{
                id: "ui_user_edit_btn",
                iconCls: 'icon-edit',
                text: '修 改',
                handler: function () {
                    $("#ui_user_editform").form("submit", {
                        url: "ashx/rm_user.ashx",
                        onSubmit: function (param) {
                            $('#ui_user_edit_btn').linkbutton('disable');   //点击就禁用按钮，防止连击
                            param.action = 'edit';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_user_edit_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = eval('(' + data + ')');    //转成json格式
                            if (dataJson.success) {
                                $("#ui_user_edit_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "修改成功！");
                                $("#ui_user_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_user_edit_btn').linkbutton('enable');    //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_user_id_edit").val(row.id);
                $("#ui_user_username_edit").textbox("setValue", row.userName);
                $("#ui_user_realname_edit").textbox("setValue", row.realName);
                $("#ui_user_points_edit").numberbox("setValue", row.points);
                $("#ui_user_tel_edit").textbox("setValue", row.tel);
                if (row.status) {
                    $("#ui_user_status_edit").attr("checked", "checked");
                }
                $("#ui_user_description_edit").val(row.description);
            },
            onClose: function () {
                $("#ui_user_edit_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    //删除用户（可批量）
    function ui_user_delete() {
        var rows = $("#ui_user_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的用户");
            return;
        }
        $.messager.confirm('提示', '确定删除勾选的这' + rows.length + '个用户？', function (r) {
            if (r) {
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();

                var ids = [];
                $.each(rows, function (i, row) {
                    ids.push(row.id);
                });
                para.ids = ids.join(",");
                $.ajax({
                    url: "ashx/rm_user.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", "删除成功！");
                            $("#ui_user_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }
    //部门设置（可批量）
    function ui_user_setdept() {
        var rows = $("#ui_user_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要设置部门的用户");
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_user_setdept_dialog",
            href: "html/ui_user_setdept.html",
            title: rows.length == 1 ? "设置部门" : "批量设置部门：" + rows.length + "个用户",
            iconCls: "icon-group",
            height: 220,
            width: 380,
            modal: true,
            buttons: [{
                id: "ui_user_setdept_btn",
                text: '确 定',
                handler: function () {
                    $("#ui_user_setdeptform").form("submit", {
                        url: "ashx/rm_user.ashx",
                        onSubmit: function (param) {
                            $('#ui_user_setdept_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'setdept';
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_user_setdept_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", " 部门设置成功！");
                                $("#ui_user_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_user_setdept_btn').linkbutton('enable');   //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                if (rows.length == 1) {   //如果是设置一个用户就自动勾选已经有的部门
                    $("#ui_user_setdept_userid").val(row.id);
                    $("#ui_user_setdept_username").val(row.userName);
                    $('#ui_user_setdept_dept').combotree('setValues', row.deptIds);
                }
                else {
                    var userids = "";
                    var usernames = "";
                    for (var i = 0; i < rows.length; i++) {
                        userids += rows[i].id + ",";
                        usernames += rows[i].userName + ",";
                    }
                    $("#ui_user_setdept_userid").val(userids.substring(0, userids.length - 1));
                    $("#ui_user_setdept_username").val(usernames.substring(0, usernames.length - 1));
                }
            },
            onClose: function () {
                $("#ui_user_setdept_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }

    //角色设置（可批量）
    function ui_user_setrole() {
        var rows = $("#ui_user_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要设置角色的用户");
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_user_setrole_dialog",
            href: "html/ui_user_setrole.html",
            title: rows.length == 1 ? "设置角色" : "批量设置角色：" + rows.length + "个用户",
            iconCls: "icon-key_go",
            height: 220,
            width: 380,
            modal: true,
            buttons: [{
                id: "ui_user_setrole_btn",
                text: '确 定',
                handler: function () {
                    $("#ui_user_setroleform").form("submit", {
                        url: "ashx/rm_user.ashx",
                        onSubmit: function (param) {
                            $('#ui_user_setrole_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'setrole';
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_user_setrole_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "角色设置成功！");
                                $("#ui_user_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_user_setrole_btn').linkbutton('enable');   //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                if (rows.length == 1) {   //如果是设置一个用户就自动勾选已经有的角色
                    $('#ui_user_setrole_role').combobox('setValues', row.roleIds);
                    $("#ui_user_setrole_userid").val(row.id);
                    $("#ui_user_setrole_username").val(row.userName);
                }
                else {
                    var userids = "";
                    var usernames = "";
                    for (var i = 0; i < rows.length; i++) {
                        userids += rows[i].id + ",";
                        usernames += rows[i].userName + ",";
                    }
                    $("#ui_user_setrole_userid").val(userids.substring(0, userids.length - 1));
                    $("#ui_user_setrole_username").val(usernames.substring(0, usernames.length - 1));
                }
            },
            onClose: function () {
                $("#ui_user_setrole_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_user_searchdata() {
        var whereStr = "1=1";
        var uname = $('#ui_user_search').find('[name=ui_user_username]').val();
        var rname =  $('#ui_user_search').find('[name=ui_user_realname]').val();
        var able = $('#ui_user_search').find('[name=ui_user_status]').val();
        var stime = $('#ui_user_adddatestart').datebox('getValue');
        var etime = $('#ui_user_adddateend').datebox('getValue');
        if (uname != "") {
            whereStr += " and userName='" + uname + "'";
        }
        if (rname != "") {
            whereStr += " and realName='" + rname + "'";
        }
        if (able != "select" && able != "") {
            whereStr += " and able=" + able + "";
        }
        if (stime != "") {
            whereStr += " and addDate > '" + stime + "'";
        }
        if (etime != "") {
            whereStr += " and addDate < '" + etime + "'";
        }
        $("#ui_user_dg").datagrid('load', {where:whereStr});
        $("#ui_user_dg").datagrid('clearSelections').datagrid('clearChecked');
        //$('#ui_user_layout').layout('collapse', 'east');    //隐藏
    }
    function ui_user_cleardata() {
        $('#ui_user_search input').val('');
        $('#ui_user_search select').val('select');
        $('#ui_user_adddatestart').datebox('setValue', '');
        $('#ui_user_adddateend').datebox('setValue', '');
        $("#ui_user_dg").datagrid('load', {});

        $("#ui_user_dg").datagrid('clearSelections').datagrid('clearChecked');
        //$('#ui_user_layout').layout('collapse', 'east');    //隐藏
    }

    //上传导入数据
    function ui_user_import() {

        $("<div/>").dialog({
            id: "ui_user_import_dialog",
            href: "html/ui_user_import.html",
            iconCls: "icon-filter",
            title: '数据导入',
            height: 220,
            width: 420,
            modal: true,
            buttons: [{
                id: "ui_user_import_btn",
                text: '导入',
                iconCls: "icon-import",
                handler: function () {
                    $("#ui_user_importform").form("submit", {
                        url: "ashx/rm_user.ashx",
                        onSubmit: function (param) {
                            $('#ui_user_import_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'import';
                            param.op = 'import';
                            if ($(this).form('validate')) {
                                return true;
                            }
                            else {
                                $('#ui_user_import_btn').linkbutton('enable');   //恢复按钮                                
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_user_import_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_user_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_user_import_btn').linkbutton('enable');  //恢复按钮
                                $('#ui_user_importform_msg').html($('<div/>').html(dataJson.msg).text());
                                //$.show_warning("提示",  "导入失败！");
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
            },
            onClose: function () {
                $("#ui_user_import_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }

</script>
<div id="ui_user_layout" class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'west',split:true,border:true,collapsed:true" title="多条件查询"
        style="width: 280px;">
        <div id="ui_user_search">
            <table class="tableForm" style="width: 100%; height: 100%; background: #F5F5F5;">
                <tr>
                    <th>登录名：
                    </th>
                    <td>
                        <input name="ui_user_username" class="easyui-textbox"  autocomplete="off" style="width: 130px;" />
                    </td>
                </tr>
                <tr>
                    <th>姓名：
                    </th>
                    <td>
                        <input name="ui_user_realname" class="easyui-textbox"  autocomplete="off" style="width: 130px;" />
                    </td>
                </tr>                
                <tr>
                    <th>是否启用：
                    </th>
                    <td>
                        <select name="ui_user_status" class="easyui-combobox" data-options="panelHeight:65,editable:false" >
                            <option value="select">请选择</option>
                            <option value="1">已启用</option>
                            <option value="0">未启用</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>添加时间：
                    </th>
                    <td>
                        <input name="ui_user_adddatestart" id="ui_user_adddatestart" class="easyui-datebox"
                            editable="false" style="width: 140px;" />
                    </td>
                </tr>
                <tr>
                    <th>至：
                    </th>
                    <td>
                        <input name="ui_user_adddateend" id="ui_user_adddateend" class="easyui-datebox"
                            editable="false" style="width: 140px;" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-search" plain="true"
                            onclick="ui_user_searchdata();">搜索</a>
                    </td>
                    <td>
                        <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-clear" plain="true"
                            onclick="ui_user_cleardata();">清空条件</a>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div data-options="region:'center',border:false">
        <table id="ui_user_dg" data-options="fit:true,border:false">
        </table>
    </div>
</div>
